package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.ui.base.JaBaseActivity;
import com.ja.openchat.views.rangeseekbar.OnRangeChangedListener;
import com.ja.openchat.views.rangeseekbar.RangeSeekBar;
import com.zy.openchat.Constants;
import com.zy.openchat.util.CacheDataManager;
import com.zy.openchat.util.SPUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MemoryDosageActivity extends JaBaseActivity {
    @BindView(R.id.cacheTimeSeekBar)
    RangeSeekBar cacheTimeSeekBar;
    @BindView(R.id.maxCacheSeekBar)
    RangeSeekBar maxCacheSeekBar;
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    @BindView(R.id.appMemoryText)
    TextView appMemoryText;

    @Override
    public void initData() {
        setTitle(getString(R.string.storage_usage));
        calcuMemory();
        int cacheTime = SPUtils.getInt(this, Constants.SPU_FOR_CACHE_SAVE_TIME,Constants.CACHE_FOR_PERMANENT);
        cacheTimeSeekBar.setProgress(cacheTime);
        cacheTimeSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                if(!isFromUser){
                    SPUtils.putInt(MemoryDosageActivity.this,Constants.SPU_FOR_CACHE_SAVE_TIME,(int)leftValue);
                }
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });

        int maxCacheTime = SPUtils.getInt(this, Constants.SPU_FOR_MAX_CACHE_OCCUPY,Constants.CACHE_FOR_PERMANENT);
        maxCacheSeekBar.setProgress(maxCacheTime);
        maxCacheSeekBar.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                if(!isFromUser){
                    SPUtils.putInt(MemoryDosageActivity.this,Constants.SPU_FOR_MAX_CACHE_OCCUPY,(int)leftValue);
                }
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });
    }

    private void calcuMemory(){
        long totalSize = CacheDataManager.getInternalMemorySize();
        long usedSize = CacheDataManager.getAvailableInternalMemorySizeValue();
        float rate = ((totalSize-usedSize)/Float.parseFloat(String.valueOf(totalSize)));
        progressBar.setProgress(((int)(rate*100)));
        appMemoryText.setText(String.format("%s %s：%s      %s：%s"
                ,getString(R.string.app_name)
                ,getString(R.string.cache_text)
                ,getString(R.string.phone_free_space)
                ,CacheDataManager.getTotalCacheSize(this)
                ,CacheDataManager.getAvailableInternalMemorySize()));
    }

    @OnClick({R.id.cleanMemoryBtn})
    void onClick(View view) {
        switch (view.getId()){
            case R.id.cleanMemoryBtn:
                clearCache();
                break;
        }
    }

    private Disposable disposable;
    public void clearCache() {
        showProgress();
        disposable = Observable.timer(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                hideProgress();
                CacheDataManager.clearAllCache(getApplication());
                calcuMemory();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(disposable!=null&&!disposable.isDisposed()){
            disposable.dispose();
        }
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_memory_dosage;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    protected boolean isHaveButterKnife() {
        return true;
    }
}
